/********************************************************************************************************
 * @file    zb_appCb.c
 *
 * @brief   This is the source file for zb_appCb
 *
 * @author  Zigbee Group
 * @date    2021
 *
 * @par     Copyright (c) 2021, Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
 *			All rights reserved.
 *
 *          Licensed under the Apache License, Version 2.0 (the "License");
 *          you may not use this file except in compliance with the License.
 *          You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 *          Unless required by applicable law or agreed to in writing, software
 *          distributed under the License is distributed on an "AS IS" BASIS,
 *          WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *          See the License for the specific language governing permissions and
 *          limitations under the License.
 *
 *******************************************************************************************************/

#if (__PROJECT_TL_CONTACT_SENSOR__)

/**********************************************************************
 * INCLUDES
 */
#include "tl_common.h"
#include "zb_api.h"
#include "zcl_include.h"
#include "bdb.h"
#include "ota.h"
#include "sampleSensor.h"
#include "app_ui.h"

/**********************************************************************
 * LOCAL CONSTANTS
 */


/**********************************************************************
 * TYPEDEFS
 */


/**********************************************************************
 * LOCAL FUNCTIONS
 */
void zbdemo_bdbInitCb(u8 status, u8 joinedNetwork);
void zbdemo_bdbCommissioningCb(u8 status, void *arg);
void zbdemo_bdbIdentifyCb(u8 endpoint, u16 srcAddr, u16 identifyTime);


/**********************************************************************
 * LOCAL VARIABLES
 */
bdb_appCb_t g_zbDemoBdbCb = 
{
	zbdemo_bdbInitCb, 
	zbdemo_bdbCommissioningCb, 
	zbdemo_bdbIdentifyCb, 
	NULL
};



/**********************************************************************
 * FUNCTIONS
 */
s32 sampleSensor_bdbNetworkSteerStart(void *arg){
	printf("bdbNetworkSteerStart\n");
	bdb_networkSteerStart();

	return -1;
}

s32 sampleSensor_bdbNetworkRejoinReq(void *arg){
	printf("bdbNetworkRejoinReq\n");
	zb_rejoinReq(zb_apsChannelMaskGet(), g_bdbAttrs.scanDuration);

	return -1;
}

s32 sampleSensor_stopPoll(void *arg){
	printf("stopPoll\n");
	zb_setPollRate(0);

	return -1;
}
/*********************************************************************
 * @fn      zbdemo_bdbInitCb
 *
 * @brief   application callback for bdb initiation
 *
 * @param   status - the status of bdb init BDB_INIT_STATUS_SUCCESS or BDB_INIT_STATUS_FAILURE
 *
 * @param   joinedNetwork  - 1: node is on a network, 0: node isn't on a network
 *
 * @return  None
 */
void zbdemo_bdbInitCb(u8 status, u8 joinedNetwork){
	printf("zbdemo_bdbInitCb:%d\n",status);
	if(status == BDB_INIT_STATUS_SUCCESS){

		if(joinedNetwork){
			light_blink_start(10, 200, 200);
			printf("joinedNetwork");
			zb_setPollRate(POLL_RATE);
			TL_ZB_TIMER_SCHEDULE(GXHT30_periodicMeas, NULL, GXHT30_MEAS_INTERVAL);
			TL_ZB_TIMER_SCHEDULE(battery_periodicMeas, NULL, BATTERY_MEAS_INTERVAL);
			TL_ZB_TIMER_SCHEDULE(sampleSensor_stopPoll, NULL, STOP_POLL_TIME);
		}else{
			light_blink_start(10, 1000, 1000);
			u16 jitter = 5000;

			TL_ZB_TIMER_SCHEDULE(sampleSensor_bdbNetworkSteerStart, NULL, jitter);
		}
	}else{
		if(joinedNetwork){
			zb_rejoinReqWithBackOff(zb_apsChannelMaskGet(), g_bdbAttrs.scanDuration);
		}
	}
}

/*********************************************************************
 * @fn      zbdemo_bdbCommissioningCb
 *
 * @brief   application callback for bdb commissioning
 *
 * @param   status - the status of bdb commissioning
 *
 * @param   arg
 *
 * @return  None
 */
void zbdemo_bdbCommissioningCb(u8 status, void *arg){
	printf("bdbCommissioningCb:%d ",status);

	switch(status){
		case BDB_COMMISSION_STA_SUCCESS:
			printf("BDB_COMMISSION_STA_SUCCESS");
			light_blink_stop();
			light_blink_start(5, 200, 200);

			zb_setPollRate(POLL_RATE);
			TL_ZB_TIMER_SCHEDULE(GXHT30_periodicMeas, NULL, GXHT30_MEAS_INTERVAL);
			TL_ZB_TIMER_SCHEDULE(battery_periodicMeas, NULL, BATTERY_MEAS_INTERVAL);
			TL_ZB_TIMER_SCHEDULE(sampleSensor_stopPoll, NULL, STOP_POLL_TIME);
			break;
		case BDB_COMMISSION_STA_IN_PROGRESS:
			break;
		case BDB_COMMISSION_STA_NOT_AA_CAPABLE:
			break;
		case BDB_COMMISSION_STA_NO_NETWORK:
		case BDB_COMMISSION_STA_TCLK_EX_FAILURE:
		case BDB_COMMISSION_STA_TARGET_FAILURE:
			{
				light_blink_start(10, 1000, 1000);
				printf("BDB_COMMISSION_STA_NO_NETWORK\n");
				u16 jitter = 5000;

				if(nwkRetryTime >= NWK_RETRY_TIMES)
				{
					light_blink_stop();
					printf("STA TARGET FAILURE overtime\n");
					drv_pm_sleep(PM_SLEEP_MODE_DEEPSLEEP,PM_WAKEUP_SRC_PAD,0);
				}else{
					nwkRetryTime++;
					TL_ZB_TIMER_SCHEDULE(sampleSensor_bdbNetworkSteerStart, NULL, jitter);
				}
			}
			break;
		case BDB_COMMISSION_STA_FORMATION_FAILURE:
			break;
		case BDB_COMMISSION_STA_NO_IDENTIFY_QUERY_RESPONSE:
			break;
		case BDB_COMMISSION_STA_BINDING_TABLE_FULL:
			break;
		case BDB_COMMISSION_STA_NO_SCAN_RESPONSE:
			break;
		case BDB_COMMISSION_STA_NOT_PERMITTED:
			break;
		case BDB_COMMISSION_STA_PARENT_LOST:
			printf("BDB_COMMISSION_STA_PARENT_LOST\n");
			//zb_rejoinSecModeSet(REJOIN_INSECURITY);
			zb_rejoinReq(zb_apsChannelMaskGet(), g_bdbAttrs.scanDuration);
			break;
		case BDB_COMMISSION_STA_REJOIN_FAILURE:
			printf("BDB_COMMISSION_STA_REJOIN_FAILURE\n");

			if(nwkRetryTime >= NWK_RETRY_TIMES)
			{
				printf("REJOIN FAILURE overtime\n");
				drv_pm_sleep(PM_SLEEP_MODE_DEEPSLEEP,PM_WAKEUP_SRC_PAD,0);
			}else{
				nwkRetryTime++;
				TL_ZB_TIMER_SCHEDULE(sampleSensor_bdbNetworkRejoinReq, NULL, nwkRetryTime*60*1000);

			}
			break;
		default:
			break;
	}
}


extern void sampleSensor_zclIdentifyCmdHandler(u8 endpoint, u16 srcAddr, u16 identifyTime);
void zbdemo_bdbIdentifyCb(u8 endpoint, u16 srcAddr, u16 identifyTime){
	sampleSensor_zclIdentifyCmdHandler(endpoint, srcAddr, identifyTime);
}





/*********************************************************************
 * @fn      sampleSensor_leaveCnfHandler
 *
 * @brief   Handler for ZDO Leave Confirm message.
 *
 * @param   pRsp - parameter of leave confirm
 *
 * @return  None
 */
void sampleSensor_leaveCnfHandler(nlme_leave_cnf_t *pLeaveCnf)
{
    if(pLeaveCnf->status == SUCCESS){
    	//SYSTEM_RESET();
    }
}

/*********************************************************************
 * @fn      sampleSensor_leaveIndHandler
 *
 * @brief   Handler for ZDO leave indication message.
 *
 * @param   pInd - parameter of leave indication
 *
 * @return  None
 */
void sampleSensor_leaveIndHandler(nlme_leave_ind_t *pLeaveInd)
{
    printf("sampleSensor_leaveIndHandler, rejoin = %d\n", pLeaveInd->rejoin);
    //printfArray(pLeaveInd->device_address, 8);
}


#endif  /* __PROJECT_TL_CONTACT_SENSOR__ */
